/* Sentinel - IRC Statistical and Operator Services
** datadump.c - Raw data dump function
**
** Copyright W. Campbell and others.  See README for more details
** Some code Copyright: Jonathan George, Kai Seidler, ircd-hybrid Team,
**                      IRCnet IRCD developers.
**
** $Id: datadump.c,v 1.22 2004/01/06 15:12:49 wcampbel Exp $
*/

#include "stats.h"
#include "struct.h"
#include "proto.h"
#include "slog.h"

void datadump(char *filename)
{
  FILE *fp = fopen(filename, "w");
  struct Server *s;
  int i;
  dlink_node *dl;

  if (!fp)
  {
    slog(DEFAULT_LOG, L_CRIT, "Unable to open data dump file %s (%s)", filename,
         strerror(errno));
    return ;
  }

  fprintf(fp, "NETINFO %u %ld %ld %ld %d %d %ld %d %d %ld %d %d "
          "%ld %d %ld %ld %ld %ld\n",
          netinfo.away, netinfo.users, netinfo.maxusers,
          (long)netinfo.t_maxusers, netinfo.opers, netinfo.maxopers,
          (long)netinfo.t_maxopers, netinfo.servers, netinfo.maxservers,
          (long)netinfo.t_maxservers,
          /* Was services context */
          0, 0, 0L, netinfo.maxaway, (long)netinfo.t_maxaway,
          netinfo.channels, netinfo.maxchannels,
          (long)netinfo.t_maxchannels);

  fprintf(fp, "DAILY %ld %ld %ld %d %ld %d %ld %d %ld %ld %ld\n",
          (long)CurrentTime,
          daily.users, (long)daily.t_users, daily.opers,
          (long)daily.t_opers, daily.servers, (long)daily.t_servers,
          0, 0L,
          daily.channels, (long)daily.t_channels);

  for (i = 0; i < S_TABLE_SIZE; i++)
  {
    DLINK_FOREACH(dl, serverhash[i].head)
    {
      s = dl->data;

      fprintf(fp,
              "%s %d %ld %ld %ld %d %d %ld %d %u %u %d %d %ld "
              "%d %d %ld %ld\n",
              s->name, IsSHide(s) ? 1 : 0, s->users,
              s->stats->maxusers, (long)s->stats->t_maxusers,
              s->opers, s->stats->maxopers,
              (long)s->stats->t_maxopers, s->stats->numsplits,
              s->stats->operkills, s->stats->serverkills,
              0, 0, 0L, s->away, s->stats->maxaway,
              (long)s->stats->t_maxaway,
              s->invis);
    }
  }

  fclose(fp);
}
